package com.jee.ssm.modules.evaluateUser.controller;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.model.Department;
import com.jee.ssm.model.EvaluateUser;
import com.jee.ssm.modules.department.services.DepartmentService;
import com.jee.ssm.modules.evaluateUser.services.EvaluateUserService;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.common.utils.UUIDFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;

/**
* 评价管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/evaluateUser")
public class EvaluateUserController extends AdminBaseController<EvaluateUser> {


    /**
     * 进入评价添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("evaluateUser:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/evaluateUser/add";
    }


    /**
     * 进入评价编辑页面
     * @param model 返回评价的容器
     * @param id 评价id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("evaluateUser:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",evaluateUserService.selectById(id));
        return "manager/evaluateUser/edit";
    }


    /**
     * 评价添加
     * @param evaluateUser 带id的评价对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("evaluateUser:add")
    @AdminControllerLog(description = "添加评价" )
    public Tip save(EvaluateUser evaluateUser)  {

        try {
            evaluateUserService.insert(evaluateUser);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }



    /**
     * 根据 id 修改评价
     * @param evaluateUser 带id的评价对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("evaluateUser:edit")
    @AdminControllerLog(description = "修改评价" )
    public Tip update(EvaluateUser evaluateUser) {

        try {
            evaluateUserService.updateById(evaluateUser);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除评价
     * @param id 评价id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("evaluateUser:delete")
    @AdminControllerLog(description = "删除评价" )
    public Tip delete(@RequestParam String id) {

        try {
            evaluateUserService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 评价id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("evaluateUser:delete")
    @AdminControllerLog(description = "批量删除评价" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            evaluateUserService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找评价
     * @param id 评价id
     * @return 评价对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("evaluateUser:list")
    public EvaluateUser find(@RequestParam String id) {

        return evaluateUserService.selectById(id);
    }


    /**
     * 获取评价列表 获取全部 不分页
     * @param request 请求参数
     * @return 评价列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("evaluateUser:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取评价列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 评价列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("evaluateUser:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {

        ParamMap paramMap = new ParamMap(request);
        PageInfo<Department> pageInfo = departmentService.list(new HashMap());
        modelMap.put("departmentList" ,pageInfo.getList());
        PageInfo<EvaluateUser> list = evaluateUserService.list(paramMap, page, size);
        modelMap.put("pageInfo",list);
        modelMap.putAll(paramMap);
        return "manager/evaluateUser/list";
    }

    /**
     * 描述： 导出评分详情记录
     * 创建时间：2020/10/13  YanZengBao
     * 修改时间：2020/10/13  YanZengBao 加注释
     * 修改时间：
     */
    @RequestMapping(value = "/exportEcxel")
    public void exportEcxel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ParamMap paramMap = new ParamMap(request);

        List<EvaluateUser> list = evaluateUserService.list(paramMap).getList();
        String originFileUrl = request.getServletContext().getRealPath("/download/info.xlsx");
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), EvaluateUser.class, list);
        FileOutputStream fos = new FileOutputStream(originFileUrl);
        workbook.write(fos);

        String fileName = "评分详情记录.xls";
        fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        //设置文件MIME类型
        response.setContentType(request.getServletContext().getMimeType(fileName));
        //设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        //读取文件
        InputStream in = new FileInputStream(originFileUrl);
        OutputStream out = response.getOutputStream();
        //写文件
        int b;
        while((b=in.read()) != -1) {
            out.write(b);
        }
        fos.close();
        in.close();
        out.close();
    }






    //---------------------------- property -------------------------------

    @Resource
    private EvaluateUserService evaluateUserService;
    @Resource
    private DepartmentService departmentService;

}
